package com.vincent.kdp.ui.fragment;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;

import com.vincent.baseapp.base.BaseFragment;
import com.vincent.kdp.R;
import com.vincent.kdp.adapter.NewChildPagerAdapter;
import com.vincent.kdp.ui.adapter.CommodityPagerAdapter;
import com.vincent.kdp.ui.bean.response.NewCommodityListOutput;
import com.vincent.kdp.ui.fragment.contract.NewContract;
import com.vincent.kdp.ui.fragment.presenter.NewPresenter;
import com.vincent.kdp.ui.other.MainActivity;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/5/28.
 */
public class NewFragment extends BaseFragment<NewContract.View, NewPresenter> implements NewContract.View {

    @Bind(R.id.new_tab)
    TabLayout mNewTab;
    @Bind(R.id.package_pager)
    ViewPager mPackagePager;

    //存放系列的集合
    private ArrayList<String> typeList=new ArrayList<>();
    //存储子fragment
    private ArrayList<NewChildFragment> mChildFragments;
    private NewChildPagerAdapter mAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_new;
    }

    @Override
    protected void initViewData() {
        initTabLayout();
    }

    private void initTabLayout() {

        //获取系类列表
        presenter.initView(null);

    }


    @Override
    public NewPresenter initPresenter() {
        return new NewPresenter(getActivity());
    }

    //获取系列列表成功后的回调
    @Override
    public void getTypeListSuccess(NewCommodityListOutput output) {
        if(output.getData().size()<=0){
            Toast.makeText(mContext, "暂无数据", Toast.LENGTH_SHORT).show();
            return;
        }

        if(output.getData().size()<=3){
            mNewTab.setTabMode(TabLayout.MODE_FIXED);
        }else{
            mNewTab.setTabMode(TabLayout.MODE_SCROLLABLE);
        }

        for(int x=0;x<output.getData().size();x++){
            typeList.add(output.getData().get(x).getSeriesName());
        }

        mChildFragments=new ArrayList<>();
        for(int i=0;i<typeList.size();i++){
            mChildFragments.add(NewChildFragment.getInstance(typeList.get(i),output));
        }

        mAdapter=new NewChildPagerAdapter(getFragmentManager(),mChildFragments);
        mPackagePager.setAdapter(mAdapter);
        mNewTab.setupWithViewPager(mPackagePager);

    }


    private ViewPager.OnPageChangeListener mOnPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            Lg("position = " + position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };
}
